package com.ruoyi.mes.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.mes.domain.MesTasks;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.mes.domain.MesJobDoneWorkers;
import com.ruoyi.mes.mapper.MesJobDoneMapper;
import com.ruoyi.mes.domain.MesJobDone;
import com.ruoyi.mes.service.IMesJobDoneService;

import javax.annotation.Resource;

/**
 * 完工汇报Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-12-16
 */
@Service
public class MesJobDoneServiceImpl implements IMesJobDoneService 
{
    @Resource
    private MesJobDoneMapper mesJobDoneMapper;

    /**
     * 查询完工汇报
     * 
     * @param jobDoneUuid 完工汇报主键
     * @return 完工汇报
     */
    @Override
    public MesJobDone selectMesJobDoneByJobDoneUuid(String jobDoneUuid)
    {
        return mesJobDoneMapper.selectMesJobDoneByJobDoneUuid(jobDoneUuid);
    }

    /**
     * 查询完工汇报列表
     * 
     * @param mesJobDone 完工汇报
     * @return 完工汇报
     */
    @Override
    public List<MesJobDone> selectMesJobDoneList(MesJobDone mesJobDone)
    {
        return mesJobDoneMapper.selectMesJobDoneList(mesJobDone);
    }

    /**
     * 新增时实际汇报数量
     *
     * @param mesJobDone 实际数量汇报
     * @return 实际数量
     */
    @Override
    public BigDecimal selectRemainJobDoneQty(MesJobDone mesJobDone)
    {
        return mesJobDoneMapper.selectRemainJobDoneQty(mesJobDone);
    }
    /**
     * 新增完工汇报
     * 
     * @param mesJobDone 完工汇报
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMesJobDone(MesJobDone mesJobDone)
    {
        mesJobDone.setCreateBy(SecurityUtils.getUsername());
        mesJobDone.setCreateTime(DateUtils.getNowDate());
        int rows = mesJobDoneMapper.insertMesJobDone(mesJobDone);
        insertMesJobDoneWorkers(mesJobDone);
        return rows;
    }

    /**
     * 修改完工汇报
     * 
     * @param mesJobDone 完工汇报
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMesJobDone(MesJobDone mesJobDone)
    {
        mesJobDoneMapper.deleteMesJobDoneWorkersByFkJobDoneUuid(mesJobDone.getJobDoneUuid());
        insertMesJobDoneWorkers(mesJobDone);
        return mesJobDoneMapper.updateMesJobDone(mesJobDone);
    }

    /**
     * 批量删除完工汇报
     * 
     * @param jobDoneUuids 需要删除的完工汇报主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMesJobDoneByJobDoneUuids(String[] jobDoneUuids)
    {
        mesJobDoneMapper.deleteMesJobDoneWorkersByFkJobDoneUuids(jobDoneUuids);
        return mesJobDoneMapper.deleteMesJobDoneByJobDoneUuids(jobDoneUuids);
    }

    /**
     * 删除完工汇报信息
     * 
     * @param jobDoneUuid 完工汇报主键
     * @return 结果
     */
    @Override
    public int deleteMesJobDoneByJobDoneUuid(String jobDoneUuid)
    {
        mesJobDoneMapper.deleteMesJobDoneWorkersByFkJobDoneUuid(jobDoneUuid);
        return mesJobDoneMapper.deleteMesJobDoneByJobDoneUuid(jobDoneUuid);
    }

    /**
     * 新增完工汇报（工人、工时）信息
     * 
     * @param mesJobDone 完工汇报对象
     */
    public void insertMesJobDoneWorkers(MesJobDone mesJobDone)
    {
        List<MesJobDoneWorkers> mesJobDoneWorkersList = mesJobDone.getMesJobDoneWorkersList();
        String jobDoneUuid = mesJobDone.getJobDoneUuid();
        if (StringUtils.isNotNull(mesJobDoneWorkersList))
        {
            List<MesJobDoneWorkers> list = new ArrayList<MesJobDoneWorkers>();
            for (MesJobDoneWorkers mesJobDoneWorkers : mesJobDoneWorkersList)
            {
                mesJobDoneWorkers.setFkJobDoneUuid(jobDoneUuid);
                list.add(mesJobDoneWorkers);
            }
            if (list.size() > 0)
            {
                mesJobDoneMapper.batchMesJobDoneWorkers(list);
            }
        }
    }
}
